﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DoAnBLL;
using PagedList;

namespace DoAn.Controllers
{ 
    public class SanPhamController : Controller
    {
        private BLUSIEntities db = new BLUSIEntities();
       
        public ActionResult ViewSanPham(string sp)
        {
            BLUSIEntities db = new BLUSIEntities();
            var data = (from spham in db.sanpham
                select spham).ToList();
           
            return View("Error");
        }
        //
        // GET: /SanPham/

        public ViewResult Index(int? page)
        {
            var sanpham = db.sanpham.Include("loaisanpham1").Include("nhacungcap1");
            int pageSize = 3;
            int pageNumber = (page ?? 1);

            return View(sanpham.ToList());
        }

        //
        // GET: /SanPham/Details/5

        public ViewResult Details(int id)
        {
            sanpham sanpham = db.sanpham.Single(s => s.ID_SANPHAM == id);
            return View(sanpham);
        }

        //
        // GET: /SanPham/Create

        public ActionResult Create()
        {
            ViewBag.LOAISANPHAM = new SelectList(db.loaisanpham, "ID_LOAISP", "TENLOAISP");
            ViewBag.NHACUNGCAP = new SelectList(db.nhacungcap, "ID_NHACC", "TENNHACC");
            return View();
        } 

        //
        // POST: /SanPham/Create

        [HttpPost]
        public ActionResult Create(sanpham sanpham)
        {
            if (ModelState.IsValid)
            {
                db.sanpham.AddObject(sanpham);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            ViewBag.LOAISANPHAM = new SelectList(db.loaisanpham, "ID_LOAISP", "TENLOAISP", sanpham.LOAISANPHAM);
            ViewBag.NHACUNGCAP = new SelectList(db.nhacungcap, "ID_NHACC", "TENNHACC", sanpham.NHACUNGCAP);
            return View(sanpham);
        }
        
        //
        // GET: /SanPham/Edit/5
 
        public ActionResult Edit(int id)
        {
            sanpham sanpham = db.sanpham.Single(s => s.ID_SANPHAM == id);
            ViewBag.LOAISANPHAM = new SelectList(db.loaisanpham, "ID_LOAISP", "TENLOAISP", sanpham.LOAISANPHAM);
            ViewBag.NHACUNGCAP = new SelectList(db.nhacungcap, "ID_NHACC", "TENNHACC", sanpham.NHACUNGCAP);
            return View(sanpham);
        }

        //
        // POST: /SanPham/Edit/5

        [HttpPost]
        public ActionResult Edit(sanpham sanpham)
        {
            if (ModelState.IsValid)
            {
                db.sanpham.Attach(sanpham);
                db.ObjectStateManager.ChangeObjectState(sanpham, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.LOAISANPHAM = new SelectList(db.loaisanpham, "ID_LOAISP", "TENLOAISP", sanpham.LOAISANPHAM);
            ViewBag.NHACUNGCAP = new SelectList(db.nhacungcap, "ID_NHACC", "TENNHACC", sanpham.NHACUNGCAP);
            return View(sanpham);
        }

        //
        // GET: /SanPham/Delete/5
 
        public ActionResult Delete(int id)
        {
            sanpham sanpham = db.sanpham.Single(s => s.ID_SANPHAM == id);
            return View(sanpham);
        }

        //
        // POST: /SanPham/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            sanpham sanpham = db.sanpham.Single(s => s.ID_SANPHAM == id);
            db.sanpham.DeleteObject(sanpham);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        public ActionResult LoaiSanPham(int id)
        {
           
            ViewBag.LoaiSanPham = db.loaisanpham.Single(s => s.ID_LOAISP == id).TENLOAISP;
            var dssp = from s in db.sanpham
                       where s.LOAISANPHAM == id
                       select s;

            return View(dssp.ToList());
        }

        public ActionResult TimKiem(string searhstring,string currentFilter, int? page, string ncc, string loaisp)
        {
            ViewBag.loaisp = new SelectList(db.loaisanpham, "TENLOAISP", "TENLOAISP");
            ViewBag.ncc = new SelectList(db.nhacungcap, "TENNHACC", "TENNHACC");

            if (Request.HttpMethod == "GET")
            {

                searhstring = currentFilter; //sets the currentFilter equal to Searchstring
            }
            else
            {
                page = 1;                   // defaults to page 1
            }

            ViewBag.CurrentFilter = searhstring;

            var sp = from s in db.sanpham select s;

            if (!String.IsNullOrEmpty(ncc))
                sp = sp.Where(v => v.nhacungcap1.TENNHACC == ncc);

            if (!String.IsNullOrEmpty(searhstring))
                sp = sp.Where(s => s.TENSANPHAM.ToLower().Contains(searhstring.ToLower()));

            if (!String.IsNullOrEmpty(loaisp))
                sp = sp.Where(t => t.loaisanpham1.TENLOAISP == loaisp);

            sp = sp.OrderBy(s => s.TENSANPHAM);
            int pageSize = 10;
            int pageNumber = (page ?? 1);

            return View(sp.ToPagedList(pageNumber, pageSize));
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}